package cn.devices.mapper;


import cn.devices.entity.TbDevice;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 设备表 Mapper 接口
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Repository
public interface TbDeviceMapper {

    @Insert("insert into tb_device(device_code, device_name, device_spec, device_price, device_loc, device_info, " +
            "device_status,type_id, disabled,is_borrow,device_img,is_register) \n" +
            "VALUE (#{deviceCode},#{deviceName},#{deviceSpec},#{devicePrice},#{deviceLoc},#{deviceInfo},#{deviceStatus}," +
            "#{typeId},0,0,#{deviceImg},#{isRegister})")
    int devicesAdd(TbDevice device);

    @Update("update tb_device set device_name=#{deviceName}, device_spec=#{deviceSpec}, device_price=#{devicePrice}," +
            " device_loc=#{deviceLoc}, device_info=#{deviceInfo}, device_status=#{deviceStatus},type_id=#{typeId},is_register=#{isRegister}\n" +
            " where device_code=#{deviceCode}")
    int devicesUpdate(TbDevice device);

    @Update("update tb_device set device_code=#{deviceCodeNew} where device_code=#{deviceCodeOld}")
    int deviceCodeUpdate(String deviceCodeOld, String deviceCodeNew);

    @Update("update tb_device set disabled=1 where device_code=#{deviceCode}")
    int deviceDeleteFalse(String deviceCode);

    @Select("select * from tb_device td left join tb_type tt on td.type_id = tt.type_id where disabled=0")
    List<TbDevice> tbDeviceList();

    @Select("select tb_device.*,tt.type_name  from  tb_device join tb_type tt on tb_device.type_id = tt.type_id where device_code=#{deviceCode}")
    TbDevice tbDeviceByCode(String deviceCode);
    @Select("select * from tb_device join tb_type tt on tb_device.type_id = tt.type_id where type_level=3")
    List<TbDevice> level3();

    @Select("select device_status from tb_device where device_code=#{deviceCode}")
    String deviceStatus(String deviceCode);

    @Select("select max(device_code)\n" +
            "from tb_device")
    String maxDeviceCode();

    @Select("select tb_device.*,tt.type_name  from  tb_device join tb_type tt on tb_device.type_id = tt.type_id where is_register='01' order by type_id")
    List<TbDevice> getNotRegister();

    @Select("select tb_device.*,tt.type_name  from  tb_device join tb_type tt on tb_device.type_id = tt.type_id where is_register='02' order by type_id")
    List<TbDevice> getRegister();

    @Select("select td.*\n" +
            "from tb_borrow inner join tb_device td on tb_borrow.device_code = td.device_code\n" +
            "where ret_time is null and td.device_code=#{deviceCode}")
    TbDevice getByCode(String deviceCode);
}
